package com.github.cakin.shiro.chapter22.web.controller;

import com.github.cakin.shiro.chapter22.entity.Resource;
import com.github.cakin.shiro.chapter22.entity.User;
import com.github.cakin.shiro.chapter22.service.ResourceService;
import com.github.cakin.shiro.chapter22.service.UserService;
import com.github.cakin.shiro.chapter22.web.bind.annotation.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Set;

/**
 * @className: IndexController
 * @description: 查询菜单在前台界面显示
 * @date: 2020/5/26
 * @author: cakin
 */
@Controller
public class IndexController {
    /**
     * 注入角色服务
     */
    @Autowired
    private ResourceService resourceService;
    /**
     * 注入用户服务
     */
    @Autowired
    private UserService userService;
    /**
     * 功能描述：显示首页
     *
     * @author cakin
     * @date 2020/5/26
     * @param loginUser 登录用户
     * @param model 传送给前端的数据
     * @return String 页面名称
     */
    @RequestMapping("/")
    public String index(@CurrentUser User loginUser, Model model) {
        Set<String> permissions = userService.findPermissions(loginUser.getUsername());
        List<Resource> menus = resourceService.findMenus(permissions);
        // 显示菜单给前端
        model.addAttribute("menus", menus);
        return "index";
    }
    /**
     * 功能描述：显示首页的内容区
     *
     * @author cakin
     * @date 2020/5/26
     * @return String 页面名称
     */
    @RequestMapping("/welcome")
    public String welcome() {
        return "welcome";
    }


}
